package com.example.demo.zipTest;

import java.io.*;
import java.util.zip.*;

public class UnzipExample {

    public static void main(String[] args) throws IOException {
        String zipFilePath = "D:/File/File.zip"; // 要解压的压缩包路径
        String destinationFolder = "D:/File/img"; // 解压后存放文件的目标文件夹路径

        File destDir = new File(destinationFolder);
        if (!destDir.exists()) {
            destDir.mkdirs();
        }

        try (ZipInputStream zis = new ZipInputStream(new FileInputStream(zipFilePath))) {
            byte[] buffer = new byte[4096];
            ZipEntry nextEntry;
            while ((nextEntry = zis.getNextEntry()) != null) {
                String entryName = nextEntry.getName();

                int count;
                BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(destinationFolder + "/" + entryName));
                while ((count = zis.read(buffer)) > 0) {
                    bos.write(buffer, 0, count);
                }
                bos.close();
            }

            System.out.println("成功解压压缩包！");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}